import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { resolve } from 'path';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
    const envConfig = loadEnv(mode, resolve(__dirname, './'));
    return {
        base: envConfig['VITE_BASE'],
        build: {
            outDir: envConfig['VITE_BUILD_OUTDIR'],
        },
        server: {
            host: '0.0.0.0',
            cors: true,
            proxy: {
                '/api/v1/business': {
                    target: envConfig['VITE_SERVER_BUSINESS'],
                    changeOrigin: true,
                },
                '/api/v1/model': {
                    target: envConfig['VITE_SERVER_MODEL'],
                    changeOrigin: true,
                },
                '/api/v1/account': {
                    target: envConfig['VITE_SERVER_ACCOUNT'],
                    changeOrigin: true,
                },
            },
        },
        resolve: {
            alias: {
                '@': resolve(__dirname, './src'),
            },
        },
        plugins: [
            vue(),
            AutoImport({
                include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/, /\.md$/],
                dts: true,
                imports: [
                    'vue',
                    'vue-router',
                    'pinia',
                    {
                        '@/service': [['default', 'MyService']],
                        '@/store': [['default', 'MyStore']],
                        '@/module': [['default', 'MyModule']],
                    },
                ],
                resolvers: [ElementPlusResolver(), IconsResolver({ prefix: 'Icon' })],
                // eslintrc: {
                //     enabled: true,
                //     filepath: './eslintrc-auto-import.json',
                //     globalsPropValue: true,
                // },
            }),
            Components({
                globs: ['src/components/*/index.vue'],
                resolvers: [
                    ElementPlusResolver(),
                    IconsResolver({
                        prefix: 'icon',
                        enabledCollections: ['ep'],
                    }),
                ],
            }),
            Icons({
                autoInstall: true,
            }),
            createSvgIconsPlugin({
                iconDirs: [resolve(process.cwd(), 'src/assets/svg')], // 指定需要缓存的图标文件夹
                symbolId: 'icon-[dir]-[name]', // 指定symbolId格式
            }),
        ],
    };
});
